package com.accp.ssmoa.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.accp.ssmoa.dao.DepartmentMapper;
import com.accp.ssmoa.pojo.Department;
import com.accp.ssmoa.service.DepartmentService;

@Service("departmentServiceImpl")
public class DepartmentServiceImpl implements DepartmentService {

	
	@Resource
	private DepartmentMapper departmentDAO;
	
	public List<Object> getDepartmentsTree() {
		//获取所有部门机构
		//List<Department> departments=departmentDAO.selectAllDepartments();
		//调用树形结构封装类
		//DepartmentTree departmentTree=new DepartmentTree();
		//List<Object> listTree=departmentTree.departmentList(departments);
		
		List<Department> list=departmentDAO.selectAllDepartments();
		List<Object> listTree = new ArrayList<Object>(); 
		//字符串拼接，形成json格式树形图数据
		String str = ""; 
		        for (int i = 0; i < list.size(); i++) {  
		        	Department  department = list.get(i);//遍历
		       str = "{id:'" +department.getId() + "', pId:'"+department.getParentdepartmentid()+"', name:\""+department.getName()+"\" }";//封装ztree需要格式的字符串  
		       listTree.add(str);  
		   }

		return listTree;
	}

	public List<Department> getPageDepartment(Integer startRow,
			Integer pageSize,Integer parentmentId){
	
		//分页获取部门机构信息
		List<Department> departments=departmentDAO.
				selectPageDepartments(startRow,pageSize,parentmentId);
		
		return departments;
	}

	public int getTotalsize(Integer parentmentId) {
		
		int totalsize=departmentDAO.getCount(parentmentId);
		
		return totalsize;
	}

	
	public int save(Department department) {
		
		return departmentDAO.insert(department);
		
	}

	public int update(Department department) {
		
		
		return departmentDAO.updateByPrimaryKeySelective(department);
		
	}

	public int delete(List<Integer> ids) {
		
		return departmentDAO.deleteByIds(ids);
		
	}

	
	
	public Department getDepartmentById(Integer id) {
		
		return departmentDAO.selectByPrimaryKey(id);
	}

}
